package com.hippoDocker.hystrixpayment8001.service;

import cn.hutool.core.util.IdUtil;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.concurrent.TimeUnit;

/**
 * @ClassName PaymentService
 * @Description TODO 客户端业务处理
 * @Author hippoDocker
 * @Date 2022/6/20 11:20
 **/
@Service
@Slf4j
public class PaymentService {
    //=====服务降级=====
    /**
     * @Description TODO 模拟消费服务--正常访问
     * @Author hippoDocker
     * @Date 2022/6/20 11:20
     * @Param [id]
     * @Return java.lang.String
     */
    public String paymentInfo_OK(Integer id) {
        log.info("线程池：" + Thread.currentThread().getName() + " paymentInfo_OK, id: " + id + "\t" + "OK");
        return "线程池：" + Thread.currentThread().getName() + " paymentInfo_OK, id: " + id + "\t" + "OK";
    }
    /**
     * @Description TODO 模拟消费服务--超时访问
     * @Author hippoDocker
     * @Date 2022/6/20 11:20
     * @Param [id]
     * @Return java.lang.String
     */
    @HystrixCommand(fallbackMethod = "paymentInfo_TimeoutHandler",commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "3000") //超时时间
    }) // 开启Hystrix熔断器
    public String paymentInfo_Timeout(Integer id) {
        try {
            TimeUnit.SECONDS.sleep(5);// 暂停5秒
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        log.info("线程池：" + Thread.currentThread().getName() + " paymentInfo_Timeout, id: " + id + "\t" + "Timeout");
        return "线程池：" + Thread.currentThread().getName() + " paymentInfo_Timeout, id: " + id + "\t" + "Timeout";
    }

    // 定义熔断方法--超时或者异常调用
    public String paymentInfo_TimeoutHandler(Integer id) {
        return "Fallback：线程池：" + Thread.currentThread().getName() + " paymentInfo_TimeoutHandler, id: " + id + "\t" + "TimeoutHandler，请稍后再试！";
    }


    //=====服务熔断=====
    //整体解释：在十秒中，有十次请求，请求错误率达到 60% 跳闸
    @HystrixCommand(fallbackMethod = "paymentCircuitBreaker_Fallback",commandProperties = {
            @HystrixProperty(name = "circuitBreaker.enabled", value = "true"), //是否开启断路器
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "10"), //请求次数
            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "100000"), //时间窗口期
            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "60") //失败率达到多少跳闸
    })
    public String paymentCircuitBreaker(Integer id){
        if (id<0){
            throw new RuntimeException("*****ID 不能为负");
        }
        String serialNumber = IdUtil.simpleUUID(); //等同于UUID.randomUUID().toString();生成唯一ID

        return "调用成功，流水号："+serialNumber+" 线程号："+Thread.currentThread().getName();
    }

    //服务熔断后fallback方法
    public String paymentCircuitBreaker_Fallback(@PathVariable("id") Integer id){
        return "id 不能负数，请稍后再试，/(ToT)/~~ id:"+id;
    }
}
